$(document).ready(function(e){
	createPageTab();
	createMarketProductListTab();
	readyAllCompany("companySelect","marketSelect");
	findMarketProductList(1);
});

var totalPageNum = 0;
var isDesc = "desc";
var marketProducts = null;
var curPageNum = null;
function findMarketProductList(pageNow){
	var tmp2=root+"/admin/market-product!findMarketProByPage";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"page.curPageNum":pageNow, "isDesc": isDesc, "superMarket.id":$("#marketSelect").val(), "product.name": $("#productName").val()},
		dataType:"json",
		success: function(json){
			if(marketProducts!=null) createMarketProductListTab();
			totalPageNum = json.totalPageNum;
			marketProducts = json.pageData;
			curPageNum = json.curPageNum;
			//写入系统list
			var htmlStr = '';
			for(var i=0; i<json.pageData.length; i++){
				htmlStr += '<tr><td height="24" bgcolor="#FFFFFF"><div align="center" class="STYLE1">';
				htmlStr += '<input name="checkbox" type="checkbox" class="STYLE2" value="'+json.pageData[i].id+'" />';
				htmlStr += '</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF" class="STYLE2">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+((json.curPageNum-1)*20+(i+1))+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].product.name+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].superMarket.company.name+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].superMarket.name+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].storecode+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].price+'</div></td>';
				htmlStr += '<td height="24" bgcolor="#FFFFFF">';
				htmlStr += '<div align="center" class="STYLE2 STYLE1">'+json.pageData[i].createDate.replace("T"," ")+'</div></td>';
				
				/*htmlStr += '<td height="24" bgcolor="#FFFFFF"><div align="center">';
				htmlStr += '<img src="'+root+'/admin/company/images/037.gif" width="9" height="9" />';
				htmlStr += '<span class="STYLE1"> [</span><a href="javascript:;" onclick="showUpdateMarketProduct('+json.curPageNum+','+i+')">编辑</a>';
				htmlStr += '<span class="STYLE1">]</span></div></td>';*/
				
				htmlStr += '<td height="24" bgcolor="#FFFFFF"><div align="center"><span class="STYLE2">';
				htmlStr += '<img src="'+root+'/admin/company/images/010.gif" width="9" height="9" />';
				htmlStr += '</span><span class="STYLE1">[</span><a href="javascript:;" onclick="deleteMarketProduct('+json.pageData[i].id+')">删除</a>';
				htmlStr += '<span class="STYLE1">]</span></div></td></tr>';
			}
			$("#listInfoTab").append(htmlStr);
			
			//写入分页信息
			$("#pageInfo").html('共'+json.totalCount+'条纪录，当前第'+json.curPageNum+'/'+json.totalPageNum+'页，每页'+json.numberPerPage+'条纪录');
			$("#onePage").unbind("click");
			$("#onePage").bind("click",function(){findMarketProductList(1);});
			if(json.curPageNum>1){
				$("#perPage").unbind("click");
				$("#perPage").bind("click",function(){findMarketProductList((json.curPageNum-1));});
			}
			if((json.curPageNum+1)<=json.totalPageNum){
				$("#nextPage").unbind("click");
				$("#nextPage").bind("click",function(){findMarketProductList((json.curPageNum+1));});
			}
			$("#lastPage").unbind("click");
			$("#lastPage").bind("click",function(){findMarketProductList(json.totalPageNum);});
		}
	});
}

function deleteMarketProduct(marketProductId){
	var ids = "";
	if(marketProductId==null){
		ids = getCheckBoxVal();
		if(ids==""){
			alert("请至少选择一个商品！");
			return;
		}
	}
	
	var tmp2=root+"/admin/market-product!deleteMarketProduct";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"page.curPageNum":curPageNum, "ids":ids, "superMarketProduct.id": marketProductId, "isDesc": isDesc, "superMarket.id":$("#marketSelect").val(), "product.name": $("#productName").val()},
		dataType:"json",
		success: function(json){
			$("#ck").removeAttr("checked");
			findMarketProductList(json.curPageNum);
		}
	});
}

function showSaveMarketProduct(){
	$("#backGroud").show();
	$("#addDiv").show();
	if($("#mpCompanySelect").html()==""){
		readyAllCompany("mpCompanySelect","mpMarketSelect");
		readyAllMarket("mpCompanySelect","mpMarketSelect");
	}
	if($("#mpBrandSelect").html()==""){
		readyAllBrand("mpBrandSelect","mpProductSelect");
		readyAllProduct("mpBrandSelect","mpProductSelect");
	}
}

function closeSaveMarketProduct(){
	$("#backGroud").hide();
	$("#addDiv").hide();
}

function showUpdateMarketProduct(pageNow, index){
	$("#backGroud").show();
	$("#updateDiv").show();
	if($("#mpCompanySelect_u").html()==""){
		readyAllCompany("mpCompanySelect_u","mpMarketSelect_u");
	}
	if($("#mpBrandSelect").html()==""){
		readyAllBrand("mpBrandSelect_u","mpProductSelect_u");
	}
	$("#mpCompanySelect_u").val(marketProducts[index].superMarket.company.id);
	$("#mpBrandSelect_u").val(marketProducts[index].product.brand.id);
	readyAllMarket("mpCompanySelect_u","mpMarketSelect_u");
	readyAllProduct("mpBrandSelect_u","mpProductSelect_u");
	
	$("#mpId_u").val(marketProducts[index].id);
	$("#mpStorecode_u").val(marketProducts[index].storecode);
	$("#mpPrice_u").val(marketProducts[index].price);
	$("#mpMarketSelect_u").val(marketProducts[index].superMarket.id);
	$("#mpProductSelect_u").val(marketProducts[index].product.id);
	
	$("#updateButton").bind("click",function(){updateMarketProduct(pageNow);});
}

function closeUpdateMarketProduct(){
	$("#updateMarketProductForm")[0].reset();
	$("#updateButton").unbind("click");
	$("#backGroud").hide();
	$("#updateDiv").hide();
}

function saveMarketProduct(){
	if($("#mpMarketSelect").val()=="" || $("#mpMarketSelect").val()==null){
		alert("店名不能为空");
		return;
	}
	if($("#mpProductSelect").val()=="" || $("#mpProductSelect").val()==null){
		alert("商品不能为空");
		return;
	}
	$("#addMarketProductForm").submit();
}

function updateMarketProduct(pageNow){
	if($("#mpMarketSelect_u").val()=="" || $("#mpMarketSelect_u").val()==null){
		alert("店名不能为空");
		return;
	}
	if($("#mpProductSelect_u").val()=="" || $("#mpProductSelect_u").val()==null){
		alert("商品不能为空");
		return;
	}
	var tmp2=root+"/admin/market-product!updateMarketProduct";
	$.ajax({
		url: tmp2,
		type: "POST",
		async:false,
		data:{"superMarketProduct.id":$("#mpId_u").val(),"superMarketProduct.storecode":$("#mpStorecode_u").val(),
				"superMarketProduct.price":$("#mpPrice_u").val(),"superMarket.id": $("#mpMarketSelect_u").val(), "product.id": $("#mpProductSelect_u").val()},
		dataType:"json",
		success: function(json){
			closeUpdateMarketProduct();
			if(json.code=="y"){
				findMarketProductList(pageNow);
			}else{
				alert("修改失败，请稍后重试！");
			}
		}
	});
}

function goPage(){
	var p = $("#pageNum").val();
	if(goPage_v(p)){
		findMarketProductList(p);
	}
}